package com.zr.bookcorner.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.Handler;
import android.view.MotionEvent;

import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.model.BookModel;
import com.zh.baselibrary.netutils.BaseResp;
import com.zh.baselibrary.netutils.HRetrofitNetHelper;
import com.zh.baselibrary.rxjava.RxSubscribe;
import com.zh.baselibrary.utils.LogUtils;
import com.zh.baselibrary.utils.PhoneUtil;
import com.zh.baselibrary.utils.ShareManager;
import com.zh.baselibrary.views.ToastView;
import com.zh.baselibrary.views.ads.AdsView;
import com.zh.model.ResAdsParItem;
import com.zr.bookcorner.R;
import com.zr.bookcorner.constant.Constant;
import com.zr.bookcorner.utils.ImageDownAsyncTask;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.schedulers.Schedulers;


public class AdsActivity extends BaseNoIMActivity{
    @BindView(R.id.ads_view)
    AdsView adsView;
    private List<ResAdsParItem> items = new ArrayList<>();
    BroadcastReceiver mBroadcastReceiver;
    @Override
    protected int getLayoutId() {
        return R.layout.ads_activity;
    }

    @Override
    protected void init() {
        initViews();
        initDatas();
    }
    private void initViews() {
        mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initDatas();
                    }
                }, 3000);
            }
        };
        //监听服务端推送的注册成功消息
        registerReceiver(mBroadcastReceiver, new IntentFilter(Constant.BROAT_CAST_ACTION_BARNER));
    }
    private void initDatas() {
        File file = new File(Environment.getExternalStorageDirectory() + ImageDownAsyncTask.PATH);
        if (!file.exists() || file.listFiles().length == 0) {
            getBarnerFromNet();
        } else {
            //从本地显示
            for (File s : file.listFiles()) {
                ResAdsParItem item = new ResAdsParItem();
//            item.originalUrl = "assets://ads/" + s;
                item.originalUrl = "file:///" + s.getAbsolutePath();
                items.add(item);
            }
            adsView.updateAds(items);
        }
//
    }

    private void getBarnerFromNet() {
        String schoolId = ShareManager.getValue(this, BaseConstant.DEVICE_SCHOOL_ID);
        String uuid = PhoneUtil.getUUID(this);
        new BookModel(this).getBarner(schoolId,uuid)
                .subscribeOn(Schedulers.io())//启动订阅线程
                .subscribe(
                        new RxSubscribe<BaseResp<List<ResAdsParItem>>>() {
            @Override
            public void onSuccess(BaseResp<List<ResAdsParItem>> baseResp) throws Exception {
                for (ResAdsParItem datum : baseResp.getData()) {
                    if(datum!=null) {
                        new ImageDownAsyncTask().execute(HRetrofitNetHelper.getDownloadUrl() + datum.originalUrl);
                        datum.originalUrl = HRetrofitNetHelper.getDownloadUrl() + datum.originalUrl;
                        LogUtils.e("url:"+datum.originalUrl);
                    }
                }
                if (!baseResp.getData().isEmpty()) {
                    adsView.updateAds(baseResp.getData());
                } else {
                    finish();
                }
            }

            @Override
            public void onHint(String hint) {
                ToastView.showShort(hint);
            }
        }
        );
    }
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBroadcastReceiver != null) {
            unregisterReceiver(mBroadcastReceiver);
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        finish();
        return super.onTouchEvent(event);

    }
}
